package csc309.datingapp;

import java.util.Map;
import java.sql.*;

import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.views.velocity.components.SetDirective;

import com.opensymphony.xwork2.ActionSupport;

/*
 * The Login action takes a username and password from the request
 * and authenticates those tokens agains the portfolioService objects
 * authentication methods.  If the user is valid, the user object
 * is stored in the session map. 
 * 
 * To receive the session map, this action implements the sessionAware
 * interface.  This decouples the action from the servlet API.  Of course,
 * the action could use the ActionContext to get this map, or even the 
 * underlying Servlet Session object, but these would be needless bindings 
 * to the Servlet API, certainly complicating testing. 
 */

public class Profile extends ActionSupport implements SessionAware {
	public String errormsg;

	public String getErrormsg() {
		return errormsg;
	}

	public void setErrormsg(String error) {
		this.errormsg = error;
	}

	public String execute() {

		String sessLoggedIn = (String) session.get("loggedIn");
		if (sessLoggedIn == null || sessLoggedIn.compareTo("true") != 0) {
			return "relogin";
		}

		Connection con;
		try {
			String dbUsername = "group09";
			String dbPassword = "asdasdqr3ggqq";

			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://bungle07.cs.toronto.edu", dbUsername,
					dbPassword);

			Statement sql = con.createStatement();
			
			session.put("searchedUser", getOtherUser());
			
			String sessUsername = (String) session.get("searchedUser");
			ResultSet usersResults = sql.executeQuery("SELECT * FROM group09.users WHERE email='"
							+ sessUsername + "'");
			if (usersResults != null) {
				while (usersResults.next()) {
					setFirstname(usersResults.getString("first_name"));
					setLastname(usersResults.getString("last_name"));
				}
			}
			usersResults.close();

			ResultSet detailsResults = sql
					.executeQuery("SELECT * FROM group09.details WHERE email='"
							+ sessUsername + "'");

			if (detailsResults != null) {
				while (detailsResults.next()) {
					if (detailsResults.getString("email").equals(sessUsername)) {
						setDob(detailsResults.getString("dob"));
						setGender(detailsResults.getString("gender"));
						setLocation(detailsResults.getString("city") + ", "
								+ detailsResults.getString("country"));
						setHeight(detailsResults.getString("height"));
						setEthnicity(detailsResults.getString("ethnicity"));
						setReligion(detailsResults.getString("religion"));
						setPoliticalview(detailsResults
								.getString("political_views"));

						detailsResults.close();
						sql.close();
						con.close();
						return "success";
					}
				}
			}

			sql.close();
			con.close();
			return "success"; ///////////////////////////// PROBLEM HRE
		} catch (Exception e) {
			setErrormsg(e.getMessage());
			return "exception";
		}
	}

	/* JavaBeans Properties to Receive Request Parameters */

	private String username;
	private String password;

	private String firstname;
	private String lastname;
	private String location;
	private String ethnicity;
	private String religion;
	private String politicalview;
	private String dob;
	private String height;
	private String Gender;
	private String biography;
	private String interests;

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getEthnicity() {
		return ethnicity;
	}

	public void setEthnicity(String ethnicity) {
		this.ethnicity = ethnicity;
	}

	public String getReligion() {
		return religion;
	}

	public void setReligion(String religion) {
		this.religion = religion;
	}

	public String getPoliticalview() {
		return politicalview;
	}

	public void setPoliticalview(String politicalview) {
		this.politicalview = politicalview;
	}

	public String getGender() {
		return Gender;
	}

	public void setGender(String gender) {
		Gender = gender;
	}

	public String getDob() {
		return dob;
	}

	public void setDob(String dob) {
		this.dob = dob;
	}

	public String getBiography() {
		return biography;
	}

	public void setBiography(String biography) {
		this.biography = biography;
	}

	public String getInterests() {
		return interests;
	}

	public void setInterests(String interests) {
		this.interests = interests;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public Map getSession() {
		return session;
	}

	private Map<String, String> session;

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	private String otherUser;
	

	public String getOtherUser() {
		return otherUser;
	}

	public void setOtherUser(String otherUser) {
		this.otherUser = otherUser;
	}

	public void validate() {

		// /* Check that fields are not empty */
		// if (getPassword().length() == 0) {
		// addFieldError("password", getText("password.required"));
		// }
		// if (getUsername().length() == 0) {
		// addFieldError("username", getText("username.required"));
		// }

	}

	public void setSession(Map session) {
		this.session = session;

	}

}
